﻿using System.Collections.Generic;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Cms;
using FytSoa.Core.Model.Sw;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Extensions;
using FytSoa.Service.Interfaces;
using SqlSugar;

namespace FytSoa.Service.Implements
{
    /*!
    * 文件名称：Sw_types服务接口实现
    */
    public class Sw_typesService : BaseServer<Sw_types>, ISw_typesService
    {
        /// <summary>
        /// 获得类别列表
        /// </summary>
        /// <param name="param">T</param>
        /// <returns></returns>
        public async Task<ApiResult<Page<SpectTypeDto>>> GetPageAsync(PageParm param)
        {
            var res = new ApiResult<Page<SpectTypeDto>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                res.data=await Db.Queryable<Sw_types, CmsColumn>((t,c)=>new JoinQueryInfos(JoinType.Left, t.ColumnGuid == c.Id))
                    .Select((t, c) => new SpectTypeDto() {
                        Guid=t.Guid,
                        ColumnGuid=t.ColumnGuid,
                        ColumnTitle=c.Title,
                        BrandJson=t.BrandJson,
                        SpectJson=t.SpectJson
                    })
                    .ToPageAsync(param.page, param.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 获得类别列表
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<List<SpectTypeDto>>> GetListAsync()
        {
            var res = new ApiResult<List<SpectTypeDto>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                res.data = await Db.Queryable<Sw_types, CmsColumn>((t, c) => new JoinQueryInfos(JoinType.Left, t.ColumnGuid == c.Id))
                    .Select((t, c) => new SpectTypeDto()
                    {
                        Guid = t.Guid,
                        ColumnGuid = t.ColumnGuid,
                        ColumnTitle = c.Title,
                        BrandJson = t.BrandJson,
                        SpectJson = t.SpectJson
                    })
                    .ToListAsync();
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 查询所有品牌和所有规格
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<SpectTypeBrandDto>> GetAllBrandAndType()
        {
            var res = new ApiResult<SpectTypeBrandDto>() { statusCode = (int)ApiEnum.Error };
            try
            {
                res.data = new SpectTypeBrandDto()
                {
                    brand=await Db.Queryable<Sw_brand>().OrderBy(m=>m.Sort,OrderByType.Desc).ToListAsync(),
                    spect=await Db.Queryable<Sw_spect>().OrderBy(m => m.Sort, OrderByType.Desc).ToListAsync()
                };

                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }
    }
}